//
//  BottomScrollView.m
//  scroll嵌套Table+悬停效果
//
//  Created by chenleping on 2018/6/21.
//  Copyright © 2018年 IAPTest. All rights reserved.
//

#import "BottomScrollView.h"
#import "TestMasonryViewController.h"

//模拟数据
#import "GGZFBBusinessModel.h"
#import "GGAddtion.h"

@interface BottomScrollView()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL canMove;
}
@property (nonatomic, strong) NSMutableArray <GGZFBBusinessModel *>*businessModelList;
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation BottomScrollView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canMove) name:@"contentCanMove" object:nil];
        //
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headerRefresh) name:@"needRefresh" object:nil];
        [self setUpChildControl];
        canMove = NO;   // 最开始的时候是不能进行滑动的
        [self loadData];
    }
    return self;
}
- (void)canMove{
    canMove = YES;
}
#pragma mark - 模拟数据
- (void)loadData{
    [self.businessModelList removeAllObjects];
    self.businessModelList = [NSArray gg_arrayModelWithPlistName:@"business.plist" withClassName:@"GGZFBBusinessModel"].mutableCopy;
    [self.tableView reloadData];
}

// 核心代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (canMove == NO) {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY <= 0) {
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"OtherCanMove" object:nil];  // 告诉其他滚动视图能滚动了
        canMove = NO;   // 自己不能滑动了
    }
}
- (void)setUpChildControl{
    self.backgroundColor = [UIColor yellowColor];
    self.contentSize = CGSizeMake(2 * [UIScreen mainScreen].bounds.size.width, self.bounds.size.height);
    self.pagingEnabled = YES;
    for (NSInteger i = 0; i < 2; i ++) {
        UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        [self addSubview:topView];
        if (i == 0) {
            topView.backgroundColor = [UIColor redColor];
        }else{
            topView.backgroundColor = [UIColor yellowColor];
        }
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 30,self.bounds.size.width, self.bounds.size.height - 30) style:UITableViewStylePlain];
        [self addSubview:tableView];
        self.tableView = tableView;
        tableView.delegate = self;
        tableView.dataSource = self;
        MJRefreshBackNormalFooter *footerMJ = [[MJRefreshBackNormalFooter alloc]init];
        [footerMJ setRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        self.tableView.mj_footer = footerMJ;
    }
}
#pragma mark - 自定义刷新
- (void)headerRefresh{
    //此处是可以进行刷新
//    NSLog(@"刷新");
    [self.tableView reloadData];
}
//不能进行
- (void)footerRefresh{
    GGZFBBusinessModel *model = [[GGZFBBusinessModel alloc]init];
    model.name = @"肯德基(黑马程序员店)";
    model.icon = @"kfc";
    model.distance = 10;
    model.discount = @"黑马程序员学员免费";
    model.offNum = 3;
    model.averagePrice = 1;
    model.level = 5;
    [self.businessModelList addObject:model];
    [self.tableView reloadData];
    //
//    self.tableView.contentOffset = CGPointZero;
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark --- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.businessModelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellid = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    NSString * str = [NSString stringWithFormat:@"%lu", indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"我是中国人%@", str];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TestMasonryViewController * testVC = [[TestMasonryViewController alloc] init];
    [[self ViewController].navigationController pushViewController:testVC animated:YES];
}
//?????
- (UIViewController *)ViewController
{
    UIResponder *next = [self nextResponder];
    
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

- (NSMutableArray <GGZFBBusinessModel *>*)businessModelList{
    if(!_businessModelList){
        _businessModelList = [NSMutableArray array];
    }
    return _businessModelList;
}
@end
